// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FeesTrackerStandardWorking.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using Org.OpenTrader.Framework.Forge.Events;
    using Org.OpenTrader.Framework.Forge.Interfaces;
    using Org.OpenTrader.Framework.LinqKit;

    #endregion

    /// <summary>
    /// The fees tracker Standard.
    /// </summary>
    public class FeesTrackerStandardWorking : FeesTracker
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FeesTrackerStandardWorking"/> class. 
        /// Fees tracker Constructor
        /// </summary>
        /// <param name="instrument">
        /// not null
        /// </param>
        public FeesTrackerStandardWorking(IInstrument instrument)
            : base(instrument)
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets InventoryEventDelegate.
        /// </summary>
        public override InventoryEventDelegate InventoryEventDelegate
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// The order event delegate.
        /// </summary>
        public override OrderEventDelegate OrderEventDelegate
        {
            get
            {
                return this.OnOrder;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The reset working.
        /// </summary>
        /// <returns>
        /// The reset working.
        /// </returns>
        protected bool ResetWorking()
        {
            // Check Instrument
            if (this.Instrument == null)
            {
                return false;
            }

            // Check Order Tracker
            if (this.Instrument.OrderTracker == null)
            {
                return false;
            }

            // Init to default values
            this.Fees = 0.0;

            // Get Cells 
            var predicate = PredicateBuilder.True<IOrderTrackingCell>();
            predicate = predicate.And(cell => cell.Open);

            var cells = this.Instrument.OrderTracker.GetTrackingCells(predicate);

            foreach (var cell in cells)
            {
                if (cell != null)
                {
                    this.Fees += cell.RequestedPrice * cell.RemainingQuantity * this.ExecutionFees;
                }
            }

            return true;
        }

        /// <summary>
        /// Update fees with events coming from market
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="args">
        /// </param>
        private void OnOrder(object sender, OrderEventArgs args)
        {
            if (args is OrderEventArgs.Status)
            {
                // Market Status
                this.ResetWorking();
            }
            else if (args is OrderEventArgs.Execution)
            {
                // Executions
                var eargs = args as OrderEventArgs.Execution;
                this.Fees -= eargs.Price * eargs.Quantity * this.ExecutionFees;
            }
        }

        #endregion
    }
}